package com.itbbfx.mapper;

import com.itbbfx.entity.Menu;
import com.itbbfx.entity.User;
import com.itbbfx.entity.UserRole;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

public interface UserMapper extends Mapper<User> {
    @Select("select count(1) from sys_user where loginname=#{loginname} ")
    int queryUserName(@Param("loginname") String loginname);

    @Select("select * from sys_user where loginname=#{loginname}")
    User queryLoginUser(@Param("loginname") String loginname);

    List<User> selectAllUser(@Param("user") User user);

    @Delete("delete from sys_user where userid = #{userid}")
    int deleteUser(@Param("userid") int userid);

    @Update("update sys_user set pwd=#{pwd},salt=#{salt} where userid=#{userid}")
    int resetUserPwd(@Param("userid") int userid, @Param("pwd") String pwd, @Param("salt") String salt);

    @Insert("insert into sys_user(loginname,realname,pwd,identity,address,phone,position,sex,available,salt)" +
            "values(#{user.loginname},#{user.realname},#{user.pwd},#{user.identity},#{user.address},#{user.phone}" +
            ",#{user.position},#{user.sex},#{user.available},#{user.salt})")
    int addUser(@Param("user") User user);

    @Update("update sys_user set loginname=#{user.loginname},realname=#{user.realname}," +
            "identity=#{user.identity},address=#{user.address},phone=#{user.phone}," +
            "position=#{user.position},sex=#{user.sex},available=#{user.available}," +
            "salt=#{user.salt} where userid=#{user.userid}")
    int updateUser(@Param("user") User user);

    @Select("select * from sys_menu m,sys_role_menu rm " +
            "where m.id=rm.mid and rm.rid=#{roleid}")
    List<Menu> queryUserRoleAuth(@Param("roleid")Integer roleid);

    @Delete("delete from sys_role_user where uid = #{userid}")
    int deleteUserRole(@Param("userid") int userid);

    int saveUserRole(@Param("userRole") UserRole userRole);

    @Select("select count(uid) from sys_role_user where uid = #{userid}")
    int queryUserRole(@Param("userid")Integer userid);

    @Select("select rid from sys_role_user where uid=#{userid}")
    List<Integer> initUserRole(@Param("userid")int userid);

    @Select("select count(1) from sys_user where identity=#{identity} and available=1")
    int queryUserIdentity(@Param("identity")String identity);

    @Select("select * from sys_user where userid=#{userid}")
    User queryUser(@Param("userid") Integer userid);

    @Update("update sys_user set realname=#{user.realname}," +
            "identity=#{user.identity},address=#{user.address},phone=#{user.phone}" +
            " where userid=#{user.userid}")
    int updateLoginUser(@Param("user") User user);

    @Update("update sys_user set pwd=#{user.pwd},salt=#{user.salt} where userid=#{user.userid}")
    int editPwd(@Param("user") User user);

    int countUserRole(@Param("roleid") int roleid);
}